package com.ml.carManager.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.ml.carManager.R;
import com.ml.carManager.ui.dialog.listener.OnCancelClickListener;

import java.util.Random;


public class UploadDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView cancel;
    private TextView content;
    private NumberProgressBar numberProgressBar;

    private String contentString = "正在发布，请稍后";
    private String cancleString = "取消发送";
    private boolean isShowCancelBtn = true;
    private int maxValue = 100;//进度条最大值
    private int currentValue = 0;//进度条当前值
    private long uploadSize = 0;//带上传的大小 单位kb
    private long currentSize = 0;//当前进度大小

//    private CycleUtile cycleUtile;

    public UploadDialog(Context context) {
        super(context, R.style.normal_dialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
        setContentView(R.layout.dialog_upload);
        initWidget();
        init();
    }

    private void initWidget() {
        cancel = (TextView) findViewById(R.id.dialog_upload_cancel);
        content = (TextView) findViewById(R.id.dialog_simple_hint_content);
        numberProgressBar = (NumberProgressBar) findViewById(R.id.dialog_upload_progressbar);

        setCanceledOnTouchOutside(true);
        cancel.setOnClickListener(this);
    }


    private void init() {
        setCancelable(false);
//        cycleUtile = new CycleUtile();

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                currentValue = 0;
                currentSize = 0;
                handler.removeCallbacks(runnable);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == cancel.getId()) {
            if (onCancelClickListener != null) {
                onCancelClickListener.onCancel();
            }
            dismiss();
            handler.removeCallbacks(runnable);
        }
    }

    /**
     * 设置提示语的文本
     *
     * @param contentString
     */
    public UploadDialog setContentString(String contentString) {
        this.contentString = contentString;
        return this;
    }

    /**
     * 设置取消Btn显示/隐藏
     *
     * @param isShowCancelBtn
     */
    public void isShowCancelBtn(boolean isShowCancelBtn) {
        this.isShowCancelBtn = isShowCancelBtn;
    }

    /**
     * 设置取消按钮的文本
     */
    public UploadDialog setCancleBtnTxt(String str) {
        this.cancleString = str;
        return this;
    }

    public void setProgressMax(int max) {
        maxValue = max;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setUploadSize(long uploadSize) {
        this.uploadSize = uploadSize / 1000;
        if (uploadSize > 5 * 1024 * 1024) {
            setContentString("图片附件较大，正在上传中");
        }
    }

    public void setProgress(int progress) {
        currentValue = progress;
        if (numberProgressBar != null)
            numberProgressBar.setProgress(progress);
    }

    /**
     * 停止模拟进度条
     */
    public void stopSimulationProgress() {
        handler.removeCallbacks(runnable);
    }

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (uploadSize <= 0)
                return;

            int value = new Random().nextInt(50) + 10;
            currentSize += value;
            float tmp = (currentSize / (uploadSize * 1.0f)) * 100;
            if (tmp >= 99)
                tmp = 99;
            numberProgressBar.setProgress((int) tmp);

            if (tmp >= 99) {
                handler.removeCallbacks(runnable);
                return;
            }

            handler.postDelayed(runnable, 500);
        }
    };

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

        }
    };

    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception e) {

        }
        handler.postDelayed(runnable, 500);

        numberProgressBar.setMax(maxValue);
        numberProgressBar.setProgress(0);
        content.setText(contentString);
        if (!isShowCancelBtn) {
            cancel.setVisibility(View.GONE);
        } else {
            cancel.setText(cancleString);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        handler.removeCallbacks(runnable);
    }

    private OnCancelClickListener onCancelClickListener = null;

    public UploadDialog setOnCancelClickListener(
            OnCancelClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;

        return this;
    }


}
